//
//  LKEmotionKeyboardView.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 2016/10/8.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit
let emotionKeyboardHeight: CGFloat = 220
let emotionToolBarHeight: CGFloat = 37
//设置cell的重用id
private let emotionCellID = "emotionCell"
class LKEmotionKeyboardView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setMyUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setMyUI () {
        //添加子控件
        addSubview(toolbar)
        addSubview(collectonView)
        //设置约束
        toolbar.snp.makeConstraints { (make) in
            make.bottom.right.left.equalToSuperview()
            make.height.equalTo(emotionToolBarHeight)
        }
        collectonView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(toolbar.snp.top)
        }
        //实现工具栏闭包
        toolbar.emotionBtnClickClosure = { (type)->() in
            print(type)
            print("💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗")
        }
    }
    //懒加载控件
    lazy var collectonView: UICollectionView = {
        //初始化流水布局
        let flowLayout = UICollectionViewFlowLayout()
        //设置属性
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
        flowLayout.itemSize = CGSize(width: ScreenWidth, height: emotionKeyboardHeight-emotionToolBarHeight)
        let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        //设置属性
        cv.isPagingEnabled = true
        cv.bounces = false
        //注册cell
        cv.register(LKEmotionCell.self, forCellWithReuseIdentifier: emotionCellID)
        //设置数据源代理
        cv.dataSource = self
        //设置代理
        cv.delegate = self
        return cv
    }()
    //底部工具栏
    lazy var toolbar: LKEmotionToolBar = LKEmotionToolBar()
    //pagecontroll
    private lazy var pageControll: UIPageControl = {
        let pc = UIPageControl()
        return pc
    }()
    //最近提示文字
    private lazy var recenteTipLabel : UILabel = UILabel(title: "最近使用的表情", font: 10, textColor: UIColor.orange)
}
extension LKEmotionKeyboardView: UICollectionViewDataSource,UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: emotionCellID, for: indexPath) as! LKEmotionCell
        cell.backgroundColor = UIColor.cyan
        cell.indexPath = indexPath
        return cell
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let contentOffsetX = scrollView.contentOffset.x + 0.5 * ScreenWidth
        let markPoint = CGPoint(x: contentOffsetX, y: 1)
        //确定indexPath
        let indexPath = collectonView.indexPathForItem(at: markPoint)
        //根据传过去的indexPath更新tooLBar
        toolbar.setEmotionToolBarBtnSelected(indexPath: indexPath!)
    }
}






